const http = require('../../util/http')
Page({
  onShareAppMessage() {
    return {
      title: '首页',
      path: 'entry/index/index',
    };
  },
  data: {
    baseurl: http.baseURL,
    productName:"",
    fileUrl:"",
    mode: 'widthFix',
    scrollTop: 10,
    goodsList:['','','','',''],
    scrollIntoView: 'one',
    scrollLeft: 0,
    goodsList:[],
    hasNextPage:true,
    pageNum:1,
    storeName:''
  },
  onLoad(option){
    this.getActivityInfo()
  },
  searchFun(){
    this.setData({
      pageNum:1
    })
    this.getActivityInfo('search')
  },
  getActivityInfo(type){
    if(!this.data.hasNextPage){
      return
    }
    let parms = {
      storeName:this.data.storeName,
      pageNum:this.data.pageNum,
      pageSize:10,
    }
    xhs.showLoading({
      title: '加载中',
    })
    http.get('/api/merchantType/getStoreList',parms).then(res => {
      console.log('res',res)
      xhs.hideLoading()
			if (res.code == 200) {
        res.data.list.map((item)=>{
          let list = []
          if(item.productList.length){
            item.productList.map((i,index)=>{
              if(index<3){
                list.push(i)
              }
            })
          }
          item.productList = list
        })
        if(res.data.hasNextPage){
          this.setData({
            pageNum: this.data.pageNum + 1,
            hasNextPage: res.data.hasNextPage
          })
        }else{
          this.setData({
            hasNextPage: false
          })
        }
        if(type == 'search'){
          this.setData({
            goodsList:res.data.list
          })
        }else{
          this.setData({
            goodsList:this.data.goodsList.concat(res.data.list)
          })
        }
			}
		})
  },
  openDetial(e) {
    console.log(e)
    const item = e.currentTarget.dataset.item
    xhs.navigateTo({
      url: '/entry/productDetail/index?id='+item.id
    });
  },
  openShopDetial(e) {
    console.log(e)
    const item = e.currentTarget.dataset.item
    xhs.navigateTo({
      url: '/entry/shopDetail/index?id='+item.id
    });
  },
  creareOrder(e){
    console.log(e)
    const item = e.currentTarget.dataset.item
    let parms ={
      productId:item.id
    }
    http.get('/api/xhs/xhsBuyProduct',parms).then(res => {
      console.log('res',res)
			if (res.code == 200) {
        xhs.requestGuaranteeOrderPayment({
          orderInfo: {
            payToken: res.data.pay_token,
            orderId: res.data.order_id,
          },
          success: res => {
            xhs.hideLoading()
            xhs.showToast({
              title: '支付成功'
            })
            console.log('支付结果', res);
          },
          fail: res => {
            xhs.showToast({
              title: '支付失败',
              icon: 'none'
            });
            console.log('支付结果', res);
          },
        });
      } else if(res.code == 902){
        xhs.navigateTo({
          url: "/entry/login/index"
        });
      }
		})

  },
  scroll(e) {
    console.log('获取滚动事件的详细信息e.detail：', e.detail);
    // 此处只做对scrollTop值的监听，不需要使用setData对视图层发起更新
    this.data.scrollTop = e.detail.scrollTop;
  },
  upper() {
    // xhs.showToast({
    //   title: '到顶了',
    //   icon: 'none'
    // });
    this.setData({
      pageNum: 1,
      hasNextPage: true
    })
    this.getActivityInfo('search')
  },
  changeStoreName({ detail }) {
    this.setData({
      storeName: detail.value,
    });
  },
  lower() {
    // xhs.showToast({
    //   title: '到底了',
    //   icon: 'none'
    // });
    if(this.data.hasNextPage){
      this.getActivityInfo()
    }else{
      xhs.showToast({
        title: '没有更多数据了',
        icon: 'none'
      });
    }
  },
  onChange(e) {
    console.log('swiper bindChange', e);
  },
});
